// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import './assets/styles/reset.css'
import './assets/styles/border.css'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import promise from 'es6-promise'
import store from './store'
import VuePreview from 'vue2-preview'

// axios兼容IE
promise.polyfill()
// 设置反向代理，前端请求默认发送到 http://localhost:8088
var axios = require('axios')
axios.defaults.baseURL = 'http://localhost:8088'
// 全局注册，之后可在其他组件中通过 this.$axios 发送数据
Vue.prototype.$axios = axios

Vue.use(ElementUI)
Vue.config.productionTip = false

// 默认安装vue2-preview图片查看器
Vue.use(VuePreview)

var EventBus = new Vue()

Object.defineProperties(Vue.prototype, {
  $bus: {
    get: function () {
      return EventBus
    }
  }
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})

// 全局导航钩子函数
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  // 判断该路由是否需要登录权限
  if (to.meta.logStatus) {
    if (localStorage.getItem('admin')) {
      console.log(localStorage.getItem('admin'))
      console.log(to.meta.title)
      next()
    } else {
      next('/login')
    }
  } else {
    next() // 确保一定要有next()被调用
  }
})
